<?php
class PlantHarvestAction extends SzAbstractAction
{
    protected $paramTypes = array(
        SzAbstractAction::TYPE_INT,
        SzAbstractAction::TYPE_ARRAY,
        SzAbstractAction::TYPE_INT
    );

    /**
     * @param $userId
     * @param $gridIdArr
     * @param $reqTime
     * @return SzResponse
     * @throws SzException
     */
    public function execute($userId, $gridIdArr, $reqTime)
    {
        GameService::get()->validateMultiRequestTimeDelta($userId, $reqTime);
        //跟新店铺经营数据
        $count=count($gridIdArr);
        UserShopService::get()->record($userId,1,2001,$count,$reqTime);

        //获取玩家周年庆buff
        /**
         * @var ModuleProfileInfoVoList $profileInfoList
         * @var ModuleProfileInfoVo $profileInfoFlower
         * @var ModuleProfileInfoVo $profileInfoSeed
         */
        $profileInfoList = SzPersister::get()->getVoList($userId, ModuleProfileConst::ORM_NAME_PROFILE_INFO);
        $profileInfoFlower = $profileInfoList->getElement(GameConst::USER_ANNIVERSARY_GET_FLOWER, false);
        $profileInfoSeed = $profileInfoList->getElement(GameConst::USER_ANNIVERSARY_GET_SEED, false);
        $hasBuffFlower = false;
        if ($profileInfoFlower){
            if ($profileInfoFlower->getInfo() > $reqTime){
                $hasBuffFlower = true;
            }
        }
        $hasBuffSeed = false;
        if ($profileInfoSeed){
            if ($profileInfoSeed->getInfo() > $reqTime){
                $hasBuffSeed = true;
            }
        }
        /**
         * @var PlantVoList $plantList
         * @var PlantVo $plant
         */
        $plantList = SzPersister::get()->getVoList($userId, GameConst::ORM_NAME_PLANT);
        $returnArr = array();
        $num = 0;
        foreach ($gridIdArr as $gridId){
            List($seedId ,$plant,$count,$item,$isDrop,$acNum) = PlantService::get()->harvest($userId, $gridId, $reqTime,$plantList,$hasBuffFlower,$hasBuffSeed,$profileInfoList);
            if (empty($seedId)){
                continue;
            }
            else{
                if (!isset($returnArr[$gridId])){
                    $returnArr[$gridId] = array(
                        'plant' => $plant->toArray(),
                        'count' => $count,
                    );
                    if ($isDrop) {
                        $returnArr[$gridId]['item'] = $item;
                    }
                }
                else{
                    $returnArr[$gridId]['count'] += $count;
                    if (!empty($item[0])){
                        $returnArr[$gridId]['item'][$item[0]] += $item[1];
                    }
                }
                $returnArr[$gridId]['actNum'] = $acNum;
                $num++;
            }
        }

        $profileInfo = $profileInfoList->getElement(GameConst::USER_INFO_TYPE_FLOWER_HARVEST_COUNT, false);
        if ($profileInfo) {
            $count = $profileInfo->getInfo();
            $profileInfo->setTime($reqTime);
            $profileInfo->setInfo(intval($count+$num));
            $profileInfoList->updateElement(GameConst::USER_INFO_TYPE_FLOWER_HARVEST_COUNT, $profileInfo);
            SzPersister::get()->setVoList($profileInfoList);
        } else {
            $profileInfo = new ModuleProfileInfoVo(
                $userId,
                GameConst::USER_INFO_TYPE_FLOWER_HARVEST_COUNT,
                $num,
                $reqTime,
                true
            );
            $profileInfoList->addElement($profileInfo);
            $profileInfoList->setElement(GameConst::USER_INFO_TYPE_FLOWER_HARVEST_COUNT, $profileInfo);
            SzPersister::get()->setVoList($profileInfoList);
        }

        return $this->buildResponse($returnArr);
    }
}